package com.hdd.art.filter.filter.impl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;




/**
 * @ClassName: ApiHandlerInterceptor
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author 刘章
 * @date 2020年10月12日 下午1:58:43
 */
@Component
@Slf4j
public class ApiHandlerInterceptor implements HandlerInterceptor {
	
	private static final Logger log = LoggerFactory.getLogger(ApiHandlerInterceptor.class);
	
//	@Resource
//	private IndexService        indexService;
//
	/**
	 * 进入controller方法之前
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//		log.info("拦截器");
//		String gateway_uuid = request.getHeader(SysConstant.gateway_uuid_key);
//		String client_code = request.getHeader(SysConstant.client_code_key);
//		String timestamp = request.getHeader(SysConstant.timestamp_key);
//		String token = request.getHeader(SysConstant.token_key);
//		log.info("url:" + request.getRequestURL().toString() + " gateway_uuid:" + gateway_uuid + " client_code:" + client_code + " timestamp:"+timestamp+" token:" + token + " " + handler);
//		//判断请求是否合法
//		if (ZtSpringApplication.isProduct() && !indexService.checkGatewayUuid(request, gateway_uuid))
//		{
//			response.setContentType("application/json; charset=utf-8");
//			response.getWriter().print(JSON.toJSONString(ResultValue.error(ResultCode.RC_2000, "请求不合法")));
//			response.getWriter().flush();
//			response.getWriter().close();
//			return false;
//		}
//		if (handler instanceof HandlerMethod)
//		{
//			HandlerMethod method = (HandlerMethod) handler;
//			AuthPass authPass = method.getMethodAnnotation(AuthPass.class);
//			if (authPass != null && authPass.validate() == true) return true;
//			else
//			{
//				//判断用户信息
//				AuthUser authUser = indexService.checkToken(request, token);
//				if (authUser == null)
//				{
//					response.setContentType("application/json; charset=utf-8");
//					response.getWriter().print(JSON.toJSONString(ResultValue.error(ResultCode.RC_8000, "用户未登录或Token失效")));
//					response.getWriter().flush();
//					response.getWriter().close();
//					return false;
//				}
//				request.setAttribute(SysConstant.user_id_key, authUser.getUserId());
//				request.setAttribute(AuthUser.authUser_attribute_key, authUser);
//			}
//		}
		return HandlerInterceptor.super.preHandle(request, response, handler);
	}
	
	/**
	 * 调用完controller之后，视图渲染之前
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		//HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
	}
	
	/**
	 * 整个完成之后，通常用于资源清理
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		//HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
	}
}
